package com.koron.common;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ComponentPopupMenu extends JPopupMenu{
	JComponent component;
	List<String> list;
	public ComponentPopupMenu(JComponent component,List<String> list)
	{
		super();
		this.component = component;
		setBackground(new Color(245,255,250));
		this.list = list;
		updateList();
	}
	private void updateList()
	{
		TextListener listener = new TextListener(component);
		for (String string : list) {
			JMenuItem jmi = new JMenuItem(string);
			jmi.setBackground(new Color(245,255,250));
			jmi.setActionCommand(string);
			jmi.addActionListener(listener);
			this.add(jmi);
		}
	}
	class TextListener implements ActionListener
	{
		private JComponent component;
		public TextListener(JComponent component)
		{
			this.component = component;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(component instanceof JTextField)
			{
				((JTextField)component).setText(e.getActionCommand());
			}
		}
	}
}
